package com.lframework.xingyun.api.controller.basedata.storecenter;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lframework.common.constants.ResponseConstants;
import com.lframework.common.exceptions.impl.DefaultClientException;

import com.lframework.starter.mybatis.resp.PageResult;

import com.lframework.starter.security.controller.DefaultBaseController;
import com.lframework.starter.web.resp.InvokeResult;

import com.lframework.starter.web.utils.ExcelUtil;
import com.lframework.xingyun.api.bo.basedata.storecenter.GetStoreCenterBo;
import com.lframework.xingyun.api.bo.basedata.storecenter.QueryStoreCenterBo;

import com.lframework.xingyun.api.excel.basedata.storecenter.StoreCenterImportModel;

import com.lframework.xingyun.basedata.entity.StoreCenter;
import com.lframework.xingyun.basedata.service.storecenter.IStoreCenterService;

import com.lframework.xingyun.basedata.vo.storecenter.CreateStoreCenterVo;
import com.lframework.xingyun.basedata.vo.storecenter.QueryStoreCenterVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * 仓库管理
 *
 * @author zmj
 */
@Api(tags = "仓库管理")
@Validated
@RestController
@RequestMapping("/basedata/storecenter")
public class StoreCenterController extends DefaultBaseController {

  @Autowired
  private IStoreCenterService storeCenterService;

  /**
   * 仓库列表
   */
  @ApiOperation("仓库列表")
  @PreAuthorize("@permission.valid('base-data:store-center:query','base-data:store-center:add','base-data:store-center:modify')")
  @GetMapping("/query")
  public InvokeResult<PageResult<QueryStoreCenterBo>> query(@RequestBody QueryStoreCenterVo queryStoreCenterVo,Integer pageSize,Integer pageNum) {
    InvokeResult<PageResult<QueryStoreCenterBo>> finalResult = new InvokeResult<>();
    PageResult<StoreCenter> query = storeCenterService.query((pageNum - 1) * pageSize, pageSize, queryStoreCenterVo);

    PageResult<StoreCenter> pageResult = new PageResult<>();


    pageResult.setDatas((List<StoreCenter>) query);
    BeanUtils.copyProperties(pageResult,finalResult);

    finalResult.setCode(200);
    finalResult.setMsg("成功");

    return finalResult;
  }

  /**
   * 查询仓库
   */
  @ApiOperation("查询仓库")
  @ApiImplicitParam(value = "ID", name = "id", paramType = "query", required = true)
  @PreAuthorize("@permission.valid('base-data:store-center:query','base-data:store-center:add','base-data:store-center:modify')")
  @GetMapping()
  public InvokeResult<GetStoreCenterBo> get() {

    throw new DefaultClientException("仓库查询待完成!");


  }

  /**
   * 批量停用仓库
   */
  @ApiOperation("批量停用仓库")
  @PreAuthorize("@permission.valid('base-data:store-center:modify')")
  @PatchMapping("/unable/batch")
  public InvokeResult<Void> batchUnable(
      ) {

    throw new DefaultClientException("批量停用仓库待完成!");

  }

  /**
   * 批量启用仓库
   */
  @ApiOperation("批量启用仓库")
  @PreAuthorize("@permission.valid('base-data:store-center:modify')")
  @PatchMapping("/enable/batch")
  public InvokeResult<Void> batchEnable(
      ) {

    throw new DefaultClientException("批量启用仓库待完成!");


  }

  /**
   * 新增仓库
   */
  @ApiOperation("新增仓库")
  @PreAuthorize("@permission.valid('base-data:store-center:add')")
  @PostMapping("/add")
  public InvokeResult<Void> create(@RequestBody CreateStoreCenterVo createVo) {
    if (createVo==null){
    throw new DefaultClientException(ResponseConstants.INVOKE_RESULT_ERROR_MSG_INPUT_ERROR+"，请勿传入空数据！");
    }
    String s = storeCenterService.create(createVo);

    throw new DefaultClientException(ResponseConstants.INVOKE_RESULT_SUCCESS_MSG+s);

  }

  /**
   * 修改仓库
   */
  @ApiOperation("修改仓库")
  @PreAuthorize("@permission.valid('base-data:store-center:modify')")
  @PutMapping
  public InvokeResult<Void> update() {

    throw new DefaultClientException("修改仓库待完成!");

  }

  @ApiOperation("下载导入模板")
  @PreAuthorize("@permission.valid('base-data:store-center:import')")
  @GetMapping("/import/template")
  public void downloadImportTemplate() {
    ExcelUtil.exportXls("仓库导入模板", StoreCenterImportModel.class);
  }

  @ApiOperation("导入")
  @PreAuthorize("@permission.valid('base-data:store-center:import')")
  @PostMapping("/import")
  public InvokeResult<Void> importExcel() {

    throw new DefaultClientException("导入待完成!");

  }
}
